package com.qd.panda.shop.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.cdqidi.repeat.annotation.RepeatSubmit;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.shop.order.ProductOrderDTO;
import com.qd.common.panda.domain.entity.shop.order.ShopOrderDTO;
import com.qd.common.panda.domain.entity.stroke.PayParamDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.shop.order.ProductOrderService;
import com.qd.panda.service.stroke.DisplayUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * APP_商城订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/productOrder/")
@Slf4j
public class AppProductOrderController {
    private final ProductOrderService productOrderService;

    /**
     * 创建商城订单
     *
     * @param shopOrderDTO 参数
     * @return 支付参数
     */
    @RepeatSubmit
    @PostMapping("/createShopOrder")
    public PayParamDTO createGasOrder(@Validated @RequestBody ShopOrderDTO shopOrderDTO) {
        shopOrderDTO.setUserId(SecurityUtils.getLoginUser().getUserId());
        return productOrderService.createOrder(shopOrderDTO);
    }

    /**
     * 订单列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ProductOrderDTO> page(ProductOrderDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return productOrderService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 获取订单
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ProductOrderDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return productOrderService.getByIdDto(id);
    }

    /**
     * 更新订单支付状态
     *
     * @param map 参数
     * @return 成功 or 失败
     */
    @RepeatSubmit
    @PutMapping("/updateShopOrderState")
    public ApiResultWrapper<String> updateShopOrderState(@Validated @RequestBody Map<String, Object> map) {
        Object orderIdObject = map.get("orderId");
        Object payStatusObject = map.get("payStatus");
        if (null == orderIdObject) {
            return ApiResult.error("订单ID不能为空");
        }
        if (null == payStatusObject) {
            return ApiResult.error("支付状态不能为空");
        }
        int payStatus;
        try {
            payStatus = Integer.parseInt(payStatusObject.toString());
        } catch (Exception ignored) {
            return ApiResult.error("支付状态值错误");
        }
        if (payStatus != DisplayUtil.PAY_STATUS_2 && payStatus != DisplayUtil.PAY_STATUS_3 && payStatus != DisplayUtil.PAY_STATUS_4) {
            return ApiResult.error("支付状态值错误");
        }
        productOrderService.updateShopOrderState(orderIdObject.toString(), payStatus, SecurityUtils.getLoginUser().getUserId());
        return ApiResult.success();
    }


    /**
     * 获得未支付订单的支付参数
     *
     * @param map 参数
     * @return 支付参数
     */
    @RepeatSubmit
    @PutMapping("/payShopOrder")
    public PayParamDTO payShopOrder(@Validated @RequestBody Map<String, String> map) {
        String orderId = map.get("orderId");
        if (!StringUtils.hasLength(orderId)) {
            throw new ApiException("订单ID不能为空");
        }
        return productOrderService.payShopOrder(orderId, SecurityUtils.getLoginUser().getUserId());
    }
}